/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.jitclouds.mda.examples;

import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IHypervisor;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.VirtualMachineList;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.INode;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IVirtualMachine;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.HypervisorList;
import br.ufba.lasid.v1.jitclouds.mda.MDA;
import br.ufba.lasid.v1.jitclouds.mda.providers.NodeProvider;

/**
 *
 * @author aliriosa
 */
public class NodeSample {
    
   public static void main(String[] args) throws Exception{
        MDA.startup();
        INode node0 = MDA.createNode();
        
        MDA.debug("Starting virtual machine in node " + node0.getID());
        startup(node0);
        
        MDA.BaseOptions.setProperty(MDA.NodeTag, "node1");
        INode node1 = MDA.create(NodeProvider.class);
        
        MDA.debug("Starting virtual machine in node " + node1.getID());
        startup(node1);
        
        MDA.BaseOptions.setProperty(MDA.NodeTag, "node2");
        INode node2 = MDA.create(NodeProvider.class);
        
        MDA.debug("Starting virtual machine in node " + node2.getID());
        startup(node2);
        
        MDA.debug("Stopping virtual machine in node " + node0.getID());
        shutdown(node0);
        
        MDA.debug("Stopping virtual machine in node " + node1.getID());
        shutdown(node1);
        
        MDA.debug("Stopping virtual machine in node " + node2.getID());
        shutdown(node2);               
        
                
    }
   
   public static void startup(INode node){
       HypervisorList hypervisors = node.getHypervisors();
        for(IHypervisor hypervisor : hypervisors){
            VirtualMachineList machines = hypervisor.getMachines();
            for(IVirtualMachine machine : machines){
                machine.startup();
            }
        }
               
   }

   public static void shutdown(INode node){
       HypervisorList hypervisors = node.getHypervisors();
        for(IHypervisor hypervisor : hypervisors){
            VirtualMachineList machines = hypervisor.getMachines();
            for(IVirtualMachine machine : machines){
                machine.shutdown();
            }
        }
               
   }
   
}
